//
//  ViewController.m
//  11206足球队
//
//  Created by 小白 on 16/3/3.
//  Copyright © 2016年 小白. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,copy) NSDictionary *football;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,copy) NSArray *footballKey;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //获取数据
    NSString *path = [[NSBundle mainBundle]pathForResource:@"footballdictionary" ofType:@"plist"];
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
    self.football = dic;
    //取key值并且排序
    NSArray *arr = [[dic allKeys] sortedArrayUsingSelector:@selector(compare:)];
    self.footballKey = arr;
    NSLog(@"%lu",arr.count);
    //创建tableview
    UITableView *tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 22, 320, 545)];
    [self.view addSubview:tableview];
    self.tableView = tableview;
    //声明代理
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //装
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
   
}
//有几个section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"%lu",self.footballKey.count);

    return self.footballKey.count;
    }// Default is 1 if not implemented
//每个section有几行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *key = self.footballKey[section];
    NSArray *arr = self.football[key];
//    return [[self.football[self.footballKey[section]] ].count ];
    NSInteger num = arr.count;
    return  num;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//    if (cell == nil) {
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
//    }
    NSString *keys = self.footballKey[indexPath.section];
    NSArray *arr = self.football[keys];
    
    
    cell.textLabel.text = arr[indexPath.row];
    return cell;

}
//section的头
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.footballKey[section];
}
//右侧导航
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.footballKey;
}








@end
